package jerry;

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * 
 * TCP connection ngWorldStats listener
 * 
 * 
 * @author (h0t@_G0|i, Alby87
 * 
 */
public class ListenTCP {
	
	private static File logDir;
	private static File utLogDir;
	private static String ftpHostName;
	private static File htmlsDir;
	private static String userDBDir;
    private static UsersManager usersManager;
    private static ResourceBundle bundle;	
	/**
	 * Socket listener
	 * 
	 * @param args
	 * @throws UnknownHostException
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public static void main(String[] args) throws UnknownHostException,
			IOException, InterruptedException, ClassNotFoundException {
		System.out.println("UtLogConnector 0.21 : a NgWorldStats Server Emulator");
        try {
			parseArguments(args);
		} catch (Exception e) {
			System.out
			.println("Usage java ListenTCP <tempLogDir> <UTLogDir> <htmlsDir> <dbDir>");
			return;
		}

		if (logDir.isDirectory() && utLogDir.isDirectory() && htmlsDir.isDirectory()) {
			try {
                ServerSocket socket = new ServerSocket(9999, 0);
                usersManager=new UsersManager(socket, userDBDir);
                System.out.println("Accepting connections...");
                new QuitterTCP(usersManager).start();
                while (true) {
                    Socket clientConn = socket.accept();
                    new NGStatsLogger(clientConn, logDir, utLogDir,ftpHostName, htmlsDir, usersManager).start();
                }
            }
            catch(SocketException e) {
                System.out.println("Server stopped to accept connections");
            }
		} else {
			System.out
					.println("Please specify a valid log directory \n usage java ListenTCP <tempLogDir> <UTLogDir> <htmlsDir> <dbDir>");
		}
	}

	/**
	 * Parses arguments in command line or reads from bundle
	 * 
	 * @param args object
	 * @throws IOException 
	 */
	private static void parseArguments(String[] args) throws IOException {
		if (args != null && args.length == 4) {
			logDir = new File(args[0]);
			utLogDir = new File(args[1]);
			htmlsDir = new File(args[2]);
			userDBDir = args[3];
		} else {
			bundle = ResourceBundle.getBundle("collector");
			logDir = new File(bundle.getString("temp.log.dir"));
			utLogDir = new File(bundle.getString("ut.log.dir"));
			htmlsDir = new File(bundle.getString("ut.stats.dir"));
			userDBDir = bundle.getString("ut.users.data.dir");
			try {
				ftpHostName = bundle.getString("ftp.server.addr");
			}catch(MissingResourceException e) {
				System.out.println("FTP host name not found");
			}
		}
		logDir.mkdir();
		logDir.deleteOnExit();
	}
	
	/**
	 * Method to get properties from bundle
	 * 
	 * @param propKey
	 * @return Bundle value
	 */
	public static String getProperty(String propKey) {
		try{
			return bundle.getString(propKey);
		}catch(MissingResourceException e) {
			System.out.println("Data not found fopr key "+propKey);
			return null;
		}
	}
}